package cn.xshi.sys.web;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.xshi.sys.model.SysUnit;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import cn.xshi.common.annotation.Auth;
import cn.xshi.common.base.BaseAction;
import cn.xshi.common.base.BasePage;
import cn.xshi.common.base.BaseResult;
import cn.xshi.common.base.BaseSearch;
import cn.xshi.common.entity.OauthAccountEntity;
import cn.xshi.common.util.StringUtil;
import cn.xshi.sys.service.SysUnitService;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.github.pagehelper.PageInfo;
import javax.annotation.Resource;
/**
 * @Desc 商品(产品)单位
 * @Author 邓纯杰
 * @CreateTime 2012-12-12 12:12:12
 */
@RestController
@RequestMapping("/sysUnit")
@Api(value = "商品(产品)单位API",tags = "商品(产品)单位API",description = "商品(产品)单位API")
public class SysUnitController extends BaseAction {

	@Resource
	private SysUnitService sysUnitService;
	
	/**
	* 查询商品(产品)单位列表并分页
	* @param baseSearch
	*/
	@PostMapping(value="/list")
	@Auth(value = "/sysUnit/list",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	@ApiOperation(value="查询商品(产品)单位列表并分页", notes="查询商品(产品)单位列表并分页")
	public BasePage<List<SysUnit>> getSysUnitListByCondition(@RequestBody(required=true)BaseSearch baseSearch){
		Map<String, Object> condition = baseSearch.convert();
		commonHPager(baseSearch);
		List<SysUnit> sysUnitList = sysUnitService.getSysUnitListByCondition(condition);
		for(SysUnit sysUnit : sysUnitList){
			if(!StringUtil.isEmpty(sysUnit.getCreateId())){
				OauthAccountEntity createBy = getAccount(sysUnit.getCreateId());
				if(null != createBy){
					sysUnit.setCreateBy(createBy.getName());
				}
			}
			if(!StringUtil.isEmpty(sysUnit.getUpdateId())){
				OauthAccountEntity modifiedBy = getAccount(sysUnit.getUpdateId());
				if(null != modifiedBy){
					sysUnit.setModifiedBy(modifiedBy.getName());
				}
			}
		}
		PageInfo<SysUnit> page = new PageInfo<SysUnit>(sysUnitList);
		return outPageBootStr(page,baseSearch);
	}
	
	/**
	* 查询单个商品(产品)单位
	* @param id
	*/
	@GetMapping(value="/get/{id}")
	@Auth(value = "/sysUnit/get",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	@ApiOperation(value="查询单个商品(产品)单位", notes="查询单个商品(产品)单位")
	public BaseResult<SysUnit> getSysUnitById(@PathVariable("id")String id){
		SysUnit sysUnit = sysUnitService.getSysUnitById(id);
		if(!StringUtil.isEmpty(sysUnit.getCreateId())){
			OauthAccountEntity createBy = getAccount(sysUnit.getCreateId());
			if(null != createBy){
				sysUnit.setCreateBy(createBy.getName());
			}
		}
		if(!StringUtil.isEmpty(sysUnit.getUpdateId())){
			OauthAccountEntity modifiedBy = getAccount(sysUnit.getUpdateId());
			if(null != modifiedBy){
				sysUnit.setModifiedBy(modifiedBy.getName());
			}
		}
		return outDataStr(sysUnit);
	}
	
	/**
	* 添加
	* @param sysUnit
	*/
	@PostMapping(value="/add")
	@ApiOperation(value="创建单个商品(产品)单位", notes="创建单个商品(产品)单位")
	public BaseResult addSysUnit(@RequestBody SysUnit sysUnit){
		int i = 0;
		if(null != sysUnit){
			i= sysUnitService.addSysUnit(sysUnit);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}
	
	/**
	* 修改
	* @param sysUnit
	*/
	@PutMapping(value="/update")
	@ApiOperation(value="编辑单个商品(产品)单位", notes="编辑单个商品(产品)单位")
	public BaseResult updateSysUnit(@RequestBody SysUnit sysUnit){
		int i = 0;
		if(null != sysUnit){
			i= sysUnitService.updateSysUnit(sysUnit);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}
	
	/**
	* 删除
	* @param id
	*/
	@DeleteMapping(value="/delete")
	@ApiOperation(value="删除商品(产品)单位", notes="删除单个商品(产品)单位")
	public BaseResult delSysUnit(String id){
		int i = 0;
		if(!StringUtil.isEmpty(id)){
			Map<String, Object> condition = new HashMap<String, Object>();
			condition.put("id",id.split(","));
			i= sysUnitService.delSysUnit(condition);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}
}
